package com.zhouyu.exampleThread.proAndComsu;

import lombok.Data;
import lombok.extern.log4j.Log4j2;

/**
 * @Description:水果消费者
 */
@Log4j2
@Data
public class FruitProductor {
  private FruitFactory fruitFactory;
  private ConsumerLock consumerLock;
  private ProductorLock productorLock;
  public void pro(){
    if(fruitFactory.isfull()){
      //如果工厂已经满了，唤醒所有的消费者线程
      synchronized (consumerLock){
        if(fruitFactory.isfull()){
          log.info("唤醒所有的消费者线程");
          consumerLock.notify();
        }
      }
      synchronized (productorLock){
        try {
          if(fruitFactory.isfull()){
            log.info("暂停所有的生产者线程");
            productorLock.wait();
          }
        } catch (InterruptedException e) {
          e.printStackTrace();
          throw new RuntimeException("生产线程等待失败");
        }
      }
    }else {
      String fruit= "苹果"+System.currentTimeMillis();
      fruitFactory.addFruit(fruit);
    }
  }
}
